from abc import ABC
from utils.http_client import HttpClient
from utils.config_loader import config
from utils.logger import logger


class BaseClient(ABC):
    """基础客户端类"""

    def __init__(self):
        self.http_client = HttpClient()
        self.base_url = config.get('host')
        self.account_host = config.get('account_host')

    def _build_url(self, endpoint: str) -> str:
        """构建完整URL"""
        return f"{self.base_url}{endpoint}"

    def _get_headers(self, token: str = None) -> dict:
        """获取请求头"""
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
        if token:
            headers['Cookie'] = token
        return headers

    def _handle_api_response(self, response: dict) -> dict:
        """处理API响应"""
        if not response.get('success', False):
            logger.error(f"API调用失败: {response}")
            raise Exception(f"API调用失败: {response.get('message', 'Unknown error')}")
        return response